import { put, call, takeEvery, all, fork, take } from 'redux-saga/effects';
import {
    UIM_GET_TABLE_DATA,
    uimChangeTableLoading,
    uimSetTableData,
    UIM_GO_BLACK_LIST,
    uimChangeVisible,
    UIM_GET_DETAIL_DATA,
    UIM_GET_APPLY_DATA,
    UIM_GET_CONTACTS_DATA,
    UIM_GET_OPERATOR_DATA,
    uimSetDetailData,
    uimSetApplyData,
    uimSetContactsData,
    uimSetOperatorData,
    uimChangeModalLoading
} from './actions';
import { getUserInfoManageData, goBlackList, contactsRecord, operatorRecord, applyRecord, userDetail } from '../api';
import { axios } from 'utils';
import { message } from 'antd';


function* getTableData(action) {
    yield put(uimChangeTableLoading(true));
    try{
        const res = yield call(getUserInfoManageData, action.params);
        console.log(res)
        if(Number(res.code) === 200) {
            const { data } = res;
            const obj = {
                data: data.records || [],
                pagination: {
                    total: data.totalRecords,
                    current: data.currentPage
                }
            }
            console.log(data);
            yield put(uimSetTableData(obj));
        }
    } catch (e) {
        console.log(e);
    }

    yield put(uimChangeTableLoading(false));
}

function* watchGetTableData() {
    yield takeEvery(UIM_GET_TABLE_DATA, getTableData);
}

function* goBlackListData(action) {
    const res = yield call(goBlackList, action.params);
    if(Number(res.code) === 200) {
        message.success("操作成功");
        yield put(uimChangeVisible(false));
        action.callback && action.callback();
    }
}

function* watchGoBlackListData() {
    yield takeEvery(UIM_GO_BLACK_LIST, goBlackListData)
}

function* getUserDetail(action) {
    try {
        const res = yield call(userDetail, action.params);
        if(Number(res.code) === 200) {
            const data = res['data'];
            let userInfo = data['userInfo'] || {},
                userImage = data['userImage'] || {},
                userAuthenInfo = data['userAuthenInfo'] || {},
                userThirdInfo = data['userThirdInfo'] || {};
            let obj = { ...userInfo, ...userImage, ...userAuthenInfo, ...userThirdInfo };
            yield put(uimSetDetailData(obj));
        }
    } catch (e) {

    }
}
function* watchGetUserDetail() {
    yield takeEvery(UIM_GET_DETAIL_DATA, getUserDetail);
}

function* getApplyRecord(action) {
    yield put(uimChangeModalLoading(true));
    try{
        const res = yield call(applyRecord, action.params);
        if(Number(res.code) === 200) {
            yield put(uimSetApplyData(res.data['records'] || []));
        }
    } catch (e) {

    }
    yield put(uimChangeModalLoading(false));
}
function* watchGetApplyRecord() {
    yield takeEvery(UIM_GET_APPLY_DATA, getApplyRecord);
}

function* getOperatorRecord(action) {
    yield put(uimChangeModalLoading(true));
    try {
        const res = yield call(operatorRecord, action.params);
        if(Number(res.code) === 200) {
            yield put(uimSetOperatorData(res['data'] || []));
        }
    } catch (e) {

    }
    yield put(uimChangeModalLoading(false));
}

function* watchGetOperatorRecord() {
    yield takeEvery(UIM_GET_OPERATOR_DATA, getOperatorRecord);
}

function* getContactsRecord(action) {
    yield put(uimChangeModalLoading(true));
    try {
        const res = yield call(contactsRecord, action.params);
        if(Number(res.code) === 200) {
            yield put(uimSetContactsData(res.data['records'] || []));
        }
    } catch (e) {

    }
    yield put(uimChangeModalLoading(false));
}

function* watchGetContactsRecord() {
    yield takeEvery(UIM_GET_CONTACTS_DATA, getContactsRecord);
}



export default function* root() {
    yield all([
        fork(watchGetTableData),
        fork(watchGoBlackListData),
        fork(watchGetUserDetail),
        fork(watchGetApplyRecord),
        fork(watchGetOperatorRecord),
        fork(watchGetContactsRecord)
    ])
}
